import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Request,
  UseGuards,
  UseInterceptors,
  UploadedFile, Query, DefaultValuePipe, ParseIntPipe, Delete
} from "@nestjs/common";
import { FileService } from './file.service';
import { AuthGuard } from "@nestjs/passport";
import { FileInterceptor } from "@nestjs/platform-express";

@Controller('file')
export class FileController {
  constructor(private readonly fileService: FileService) {}

  /**
   * 上传
   */
  @UseGuards(AuthGuard('jwt'))
  @Post('uploadFile')
  @UseInterceptors(FileInterceptor('file'))
  async uploadFile(@Request() req: any, @UploadedFile() file: any, @Body() body: any) {
    return this.fileService.uploadFile(req, file, body);
  }

  /**
   * 分页查询
   * @param pageNo
   * @param pageSize
   * @param username
   */
  @Get('/list')
  findWithPagination(
    @Query('pageNo', new DefaultValuePipe(1)) pageNo: number,
    @Query('pageSize', new DefaultValuePipe(10)) pageSize: number,
    @Query('username') username: string,
  ) {
    return this.fileService.findWithPagination(pageNo, pageSize, username);
  }

  /**
   * 更新
   * @param id
   * @param updateFile
   */
  @Post('/update/:id')
  updateFile(@Param('id', ParseIntPipe) id: string, @Body() updateFile:any) {
    return this.fileService.updateFile(+id, updateFile);
  }

  /**
   * 软删除
   */
  @Delete('/softDelete/:id')
  async softDeleteFile(@Param('id', ParseIntPipe) id: number) {
    await this.fileService.softDeleteFile(id);
    return { message: '删除文件成功' };
  }

  /**
   * 删除
   */
  @Delete('/remove/:id')
  async removeFile(@Param('id', ParseIntPipe) id: number) {
    await this.fileService.removeFile(id);
    return { message: '删除文件成功' };
  }
}
